package com.example.demo.demo.config;

/**
 * TODO
 *
 * @author My
 * @date 2020/12/20 14:50
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author BEE
 * @title: SwaggerConfig
 * @projectName demo
 * @description: Swagger2配置类
 * @date 2020/7/3110:34
 */
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        Contact contact = new Contact("上博思，找抗抗", "http://cqrz.iflysse.com/", "327806382@qq.com");
        return new ApiInfoBuilder()
                //页面标题
                .title("融智讯飞Springboot课程API")
                //条款地址
                .contact(contact)
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}